package com.example.demo.controller;

import com.example.demo.pojo.Course;
import com.example.demo.service.CourseService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@Transactional
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @ApiOperation(value = "根据cno删除课程")
    @RequestMapping("/deleteCourseByCno")
    public String deleteCourseByCno(@RequestParam("cno") int cno){
        courseService.deleteCourseByCno(cno);
        return "redirect:/student/getAllCourse";
    }
    @ApiOperation(value = "获取所有课程")
    @RequestMapping("/getAllCourse")
    public String getAllCourse(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5") Integer size, Model model){
        List<Course> list=courseService.getAllCourse(pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "/student/addCourse";
    }
    @ApiOperation("根据课程名字查找")
    @RequestMapping("/queryCourse")
    public String queryCourse(@RequestParam("cname") String cname,Model model){
       List<Course> course= courseService.queryCourse(cname);
       if(course!=null){
           model.addAttribute("course",course);
       }
       return "student/searchCourse";
    }
    @ApiOperation("查看课程里的学生")
    @RequestMapping("/getCourseStudent")
    public String getCourseStudent(@RequestParam("cno") int cno,Model model){
           Course course= courseService.getCourseStudent(cno);
           System.out.println(course);
           if(course.getScList()!=null){
               model.addAttribute("courses",course);
           }
           return "teacher/courseStudent";
    }
}
